
package com.wdf.dota;

import org.apache.http.entity.StringEntity;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.wdf.bean.Comment;
import com.wdf.rest.service.DotaDiscussionRestClient;

public class Dota2SubmitCommentActivity  extends Activity{
	
	Button returnCommentBtn;
	Button submitCommentBtn;
	
	TextView myUsernameView ;
	
	EditText myComment;
	
	DotaDiscussionRestService service  ;
	String topic;
	String username;
	
	String heroname;

	
	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.submit_comment);
		submitCommentBtn =  (Button) findViewById(R.id.submit_comment);
		submitCommentBtn.setOnClickListener(submitCommentButtonListener);
		
		returnCommentBtn =  (Button) findViewById(R.id.return_comment);
		returnCommentBtn.setOnClickListener(returnCommentButtonListener);
		
		myUsernameView = (TextView)this.findViewById(R.id.my_username);
		myComment =  (EditText)this.findViewById(R.id.my_comment);
		
		SharedPreferences sharedPreferences = getSharedPreferences("wonderful",
				Context.MODE_PRIVATE);
		username = sharedPreferences.getString("username", "");

		myUsernameView.setText(username+" :");
	
		Bundle bundle = this.getIntent().getExtras();
		 heroname =	bundle.getString("heroname");
		 topic ="dota2-"+ heroname;
		service  = new DotaDiscussionRestService(this);
		

	}
	
	OnClickListener submitCommentButtonListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			 Comment comment = new Comment();
              
			 String content = myComment.getText().toString();
			 if(content == null || "".equals(content.trim())){
				 Toast.makeText(Dota2SubmitCommentActivity.this, "请输入评论内容!", 2).show();
				 return ;
			 }
			 if(content.length()>=1000){
				 Toast.makeText(Dota2SubmitCommentActivity.this, "评论内容太长,超过最大限制!!", 2).show();
				 return ;
			 }
			 
			   boolean checkNickname = false;

			 comment.setNickname(username.trim());
			 comment.setContent(content);
			 comment.setTopic("dota2-"+heroname);
			 
			 service.addDiscussion(comment,checkNickname);
			 waitingDialog();
		}
	};
	
	private ProgressDialog dialog;

	private void waitingDialog() {
		dialog = new ProgressDialog(this);
		dialog.setIcon(R.drawable.smile);
		dialog.setTitle("请稍等");
		dialog.setMessage("正在添加评论.....");
		dialog.setCancelable(true);
		dialog.show();

	}
	
	
	OnClickListener returnCommentButtonListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			Dota2SubmitCommentActivity.this.finish();
		}
	};
	
	class DotaDiscussionRestService {

        private Context  context;
        
        public DotaDiscussionRestService(Context context){
        	this.context = context;
        }

      
		 public  void  addDiscussion(final Comment comment, final boolean checkNickname){
	 			final RequestParams params = new RequestParams();
				 params.put("heroname", comment.getTopic());
				 JSONObject object = new JSONObject();
				 try {
					  object.put("userid", comment.getUserid());
					 object.put("content", comment.getContent());
					 object.put("nickname", comment.getNickname());
					 object.put("supporter", comment.getSupporter());
					 object.put("topic", comment.getTopic());
					 StringEntity entity = new StringEntity(object.toString(), "UTF-8");

					 DotaDiscussionRestClient.post(Dota2SubmitCommentActivity.this, "/dota/discussions.json?checknickname="+checkNickname, entity, "application/json;charset=UTF-8", new JsonHttpResponseHandler() {
				            @Override
				            public void onSuccess(JSONObject object) {
				            	 if(object!=null ){
				            		   try {
										String nickname = object.getString("nickname");
										if(nickname !=null && nickname.length()>0){
											Toast.makeText(context, "添加评论成功!", 2).show();
							            	 SharedPreferences sharedPreferences = getSharedPreferences("wonderful",
														Context.MODE_PRIVATE);
							            		Editor editor = sharedPreferences.edit();
							            		editor.putString("nickname", nickname);
							            		editor.commit();
							            		Dota2SubmitCommentActivity.this.setResult(4, Dota2SubmitCommentActivity.this.getIntent());
							            		
							            		Dota2SubmitCommentActivity.this.finish();
							            		
										}else{
											 Toast.makeText(context, "用户昵称重复,请更换昵称!", 2).show();
										}
									} catch (JSONException e) {
										 Toast.makeText(context, "添加评论失败!", 2).show();
									}finally{
										dialog.dismiss();
									}
				            		   
				            	 }
				            	
				            }
				           public void onFailure(Throwable error){
				        	   dialog.dismiss();
				        	   Toast.makeText(context, "添加评论失败!", 2).show();
				           }
				            
				        });
 
				} catch (Exception e) {
					  dialog.dismiss();
		        	   Toast.makeText(context, "添加评论失败!", 2).show();
				}
				 
			 }	
	}
}
